/*
 * @Author: chunlai
 * @Date: 2023-12-07 08:51:58
 * @LastEditTime: 2024-09-24 15:09:09
 * @LastEditors: minsheng_insurance_agency_zhoucl
 * @FilePath: \01.latest-short-insure\src\translate\validate\customer.js
 * @Description: 客户输入信息校验规则
 */

import {
  tbgxRuleCheck,
  nameRuleCheck,
  idTypeRuleCheck,
  idFrontImgCheck,
  phoneRuleCheck,
  birthdayRuleCheck,
  noticeRuleCheck,
} from "@/translate/validate/fields.js";
import { emptyFieldCheck } from "@/methods/userinfos.js";
import { checkStrMaxNum, IdentityCodeValid } from "@/methods/userinfos.js";
import store from "@/store/index";

//常量 手机号校验
const validphone = /^1[3456789]\d{9}$/;
// 邮箱校验
const validateEmail =
  /(^[A-Za-z0-9]+([_\.][A-Za-z0-9]+)*@([A-Za-z0-9\-]+\.)+[A-Za-z]{2,6}$)|(^$)/;

const validateIdCard =
  /^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/;
/**
 * 校验客户信息
 * @param {*} userModel 字段模板
 * @param {*} infoData
 * @param {*} utype
 * @returns
 */

export const validUserInfo = (userModel, infoData, utype) => {
  let obj = {
    msg: "",
    status: true,
    target: "",
  };
  let breakTips = [];
  // 投保关系校验
  if (userModel.relationToAppnt !== undefined) {
    obj = tbgxRuleCheck(infoData, utype);
    if (!obj.status) return obj;
    if (obj.breakTips) {
      breakTips.push(obj.breakTips);
    }
  }
  // 姓名校验
  if (userModel.name !== undefined) {
    obj = nameRuleCheck(infoData.name, utype);
    if (!obj.status) return obj;
    if (obj.breakTips) {
      breakTips.push(obj.breakTips);
    }
  }
  //证件类型校验
  if (userModel.idType !== undefined) {
    obj = idTypeRuleCheck(infoData, utype);
    if (!obj.status) return obj;
    if (obj.breakTips) {
      breakTips.push(obj.breakTips);
    }
  }
  //证件号码校验
  if (userModel.idNum !== undefined) {
    obj = idNumCheck(infoData, utype);
    if (!obj.status) return obj;
    if (obj.breakTips) {
      breakTips.push(obj.breakTips);
    }
  }
  // 证件起期校验
  if (userModel.validDate !== undefined) {
    obj = emptyFieldCheck(infoData, utype, "validDate", "证件有效期起期");
    if (!obj.status) return obj;
    if (obj.breakTips) {
      breakTips.push(obj.breakTips);
    }
  }
  // 证件止期校验
  if (userModel.validDateEnd !== undefined) {
    obj = emptyFieldCheck(infoData, utype, "validDateEnd", "证件有效期止期");
    if (!obj.status) return obj;
    if (obj.breakTips) {
      breakTips.push(obj.breakTips);
    }
  }
  // 性别校验
  if (userModel.sex !== undefined) {
    obj = emptyFieldCheck(infoData, utype, "sex", "性别");
    if (!obj.status) return obj;
    if (obj.breakTips) {
      breakTips.push(obj.breakTips);
    }
  }
  // 出生日期
  if (userModel.birthDate !== undefined) {
    obj = birthdayRuleCheck(infoData, utype);
    if (!obj.status) return obj;
    if (obj.breakTips) {
      breakTips.push(obj.breakTips);
    }
  }

  // 现居详细地址校验
  if (userModel.permanentAddress !== undefined) {
    obj = addrCheck(infoData, utype, "permanentAddress");
    if (!obj.status) return obj;
    if (obj.breakTips) {
      breakTips.push(obj.breakTips);
    }
  }
  //手机号
  if (userModel.phone !== undefined) {
    obj = phoneRuleCheck(infoData, utype);
    if (!obj.status) return obj;
    if (obj.breakTips) {
      breakTips.push(obj.breakTips);
    }
  }
  if (userModel.occupationCode !== undefined) {
    obj = emptyFieldCheck(infoData, utype, "occupationCode", "职业");
    if (!obj.status) return obj;
    if (obj.breakTips) {
      breakTips.push(obj.breakTips);
    }
  }
  //社保
  if (userModel.socialInsuFlag !== undefined) {
    obj = emptyFieldCheck(infoData, utype, "socialInsuFlag", "是否有社保");
    if (!obj.status) return obj;
    if (obj.breakTips) {
      breakTips.push(obj.breakTips);
    }
  }
  //邮箱
  if (userModel.email !== undefined) {
    obj = emailCheck(infoData, utype);
    if (!obj.status) return obj;
  }
  // 个税居民类型
  if (userModel.crsIdType !== void 0) {
    obj = emptyFieldCheck(infoData, utype, "crsIdType", "个税居民类型");
    if (!obj.status) return obj;
  }
  // 证件照
  if (userModel.frontImg !== void 0) {
    obj = idFrontImgCheck(infoData, utype);
    if (!obj.status) return obj;
  }
  if (utype == "被保人") {
    // 告知校验
    if (userModel.healthyStatus !== void 0) {
      obj = noticeRuleCheck(infoData, utype);
      if (!obj.status) return obj;
    }
  }

  return {
    ...obj,
    breakTips: breakTips,
  };
};

/**
 * 保单信息校验
 * @param {*} strModel
 * @param {*} formData
 * @returns
 */

export const validBillInfo = (strModel, formData, isHasRenew) => {
  // console.log('validBillInfo', formData);
  let obj = {
    msg: "",
    status: true,
    target: "",
  };
  let breakTips = [];
  // 续保相关
  // 1.是否有选择续保配置
  if (isHasRenew == "1") {
    if (formData.renewFlag == "") {
      obj.msg = "请确认是否开通续保";
      obj.status = false;
      obj.target = "renewFlag";
      return obj;
    }
    if (formData.renewFlag == "-1") {
      const pageDic = store.state.page;
      if (pageDic.renewBank == "1") {
        if (formData.expayMode == "") {
          obj.msg = "请确认扣款方式";
          obj.status = false;
          obj.target = "expayMode";
          return obj;
        }
        if (formData.expayMode == "2") {
          if (strModel.bankAccNo !== undefined) {
            if (formData.bankAccNo !== undefined && formData.bankAccNo === "") {
              obj.msg = "请输入银行账号";
              obj.status = false;
              obj.target = "bankAccNo";
              return obj;
            }
            if (
              formData.bankAccNo.length < 10 ||
              formData.bankAccNo.length > 21
            ) {
              obj.msg = "银行卡号位数错误";
              obj.status = false;
              obj.target = "bankAccNo";
              return obj;
            }
          }
          if (strModel.bankCode !== undefined) {
            if (formData.bankCode !== undefined && formData.bankCode === "") {
              obj.msg = "请选择缴费银行";
              obj.status = false;
              obj.target = "bankcode";
              return obj;
            }
          }

          if (strModel.firBankMobile !== undefined) {
            if (formData.firBankMobile == "") {
              obj.msg = `请输入预留手机号`;
              obj.target = "firBankMobile";
              obj.status = false;
              return obj;
            }
            if (!validphone.test(formData.firBankMobile)) {
              obj.msg = `预留手机号格式错误`;
              obj.target = "firBankMobile";
              obj.status = false;
              return obj;
            }
          }
        }
      }
    }
  }

  return { ...obj, breakTips };
};

/**
 * 证件号码校验
 * @param {*} userInfo
 * @param {*} userType
 * @returns
 */
export const idNumCheck = (userInfo, userType) => {
  let obj = {
    msg: "",
    status: true,
    target: "idNum",
  };
  if (userInfo.idNum === "") {
    obj.msg = `请输入${userType}证件号码`;
    obj.status = false;
    return obj;
  }
  // 身份证-户口本 校验
  if (userInfo.idType === "0" || userInfo.idType === "3") {
    if (!IdentityCodeValid(userInfo.idNum)) {
      obj.msg = `${userType}身份证校验不通过`;
      obj.status = false;
      return obj;
    }
  } else if (userInfo.idType === "11") {
    // 出生证时-首位英文字母+9位阿拉伯数字，例如A000000001
    var bronReg = /^[A-z]{1}\d{9}$/;
    if (!bronReg.test(userInfo.idNum)) {
      obj.msg = `${userType}证件号码不正确`;
      obj.status = false;
      return obj;
    }
  } else if (userInfo.idType === "2") {
    // 军人证：需为10-18位
    if (userInfo.idNum.length < 10 || userInfo.idNum.length > 18) {
      obj.msg = `${userType}证件号码不正确`;
      obj.status = false;
      return obj;
    }
  } else if (userInfo.idType === "10") {
    // 回乡证：当国籍为“中国香港”时，号码需为“H”+8位阿拉伯数字；当国籍为“中国澳门”时，号码需为“M”+8位阿拉伯数字
    let regs1 = /^H\d{8}$/;
    if (userInfo.nationality == "MAC") {
      regs1 = /^M\d{8}$/;
    }
    if (!regs1.test(userInfo.idNum)) {
      obj.msg = `${userType}证件号码不正确`;
      obj.status = false;
      return obj;
    }
  } else if (userInfo.idType === "9") {
    // 台胞证：证件号码为8位阿拉伯数字
    let regs1 = /^[0-9]{8}$/;
    if (!regs1.test(userInfo.idNum)) {
      obj.msg = `${userType}证件号码不正确`;
      obj.status = false;
      return obj;
    }
  } else if (userInfo.idType === "16") {
    // 外国人永久居留身份证：须为15位，且需为大写字母+数字
    let regs1 = /^[A-Z]{3}\d{12}$/;
    if (!regs1.test(userInfo.idNum)) {
      obj.msg = `${userType}证件号码不正确`;
      obj.status = false;
      return obj;
    }
  } else if (userInfo.idType == "29" || userInfo.idType == "8") {
    // 港澳居民居住证
    if (!validateIdCard.test(userInfo.idNum)) {
      obj.msg = `${userType}证件号码不正确`;
      obj.status = false;
      return obj;
    }
    let starStr = "830000";
    if (userInfo.idType == "8") {
      starStr = "830000";
    } else {
      if (userInfo.nationality == "MAC") {
        starStr = "820000";
      }
      if (userInfo.nationality == "HKG") {
        starStr = "810000";
      }
    }
    if (!userInfo.idNum.startsWith(starStr)) {
      obj.msg = `${userType}证件号码不正确`;
      obj.status = false;
      return obj;
    }
  }
  return obj;
};

// 详细地址校验
export const addrCheck = (userInfo, userType, keyName) => {
  let obj = {
    msg: "",
    target: keyName,
    status: true,
  };
  const tempDic = store.state.dictionary,
    addrValue = userInfo[keyName] || "",
    maxNum = tempDic.addrSameStrMaxNum || 5;
  if (keyName == "permanentAddress" && !userInfo.province) {
    obj.msg = `请选择${userType}现居地址`;
    obj.target = "areaName";
    obj.status = false;
    return obj;
  }
  if (addrValue.length < 6) {
    obj.msg = `详细地址不少于6个字符`;
    obj.status = false;
    return obj;
  }
  if (checkStrMaxNum(addrValue, maxNum)) {
    obj.msg = `${userType}地址中同一个字（包括汉字、字母、数字）连续出现不能超过${maxNum}次`;
    obj.status = false;
    return obj;
  }
  // 地址包含关键字
  let rules2 = tempDic.detailAddrRule.split(",");
  for (let i = 0; i < rules2.length; i++) {
    if (!addLabel(addrValue, rules2[i])) {
      obj.msg = `详细地址必须包含${rules2[i]}`;
      obj.status = false;
      return obj;
    }
  }
  return obj;
};

// 地址关键字校验
function addLabel(str, ruleStr) {
  const rulesArr = ruleStr.split("/");
  let resVal = false;
  for (let i = 0; i < rulesArr.length; i++) {
    if (str.indexOf(rulesArr[i]) !== -1) {
      resVal = true;
      break;
    }
  }
  return resVal;
}

// 邮箱校验
export const emailCheck = (userInfo, userType) => {
  let obj = {
    msg: "",
    status: true,
  };
  if (!userInfo.email) {
    obj.msg = `请输入${userType}邮箱`;
    obj.status = false;
    obj.target = "email";
    return obj;
  }
  if (!validateEmail.test(userInfo.email)) {
    obj.msg = `${userType}邮箱格式错误`;
    obj.status = false;
    obj.target = "email";
    return obj;
  }
  return obj;
};
